package com.ts.movie.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ts.common.security.dto.UserToken;
import com.ts.movie.dto.ScoreDto;
import com.ts.movie.entity.MovieScore;
import com.ts.movie.mapper.MovieScoreMapper;
import com.ts.movie.service.MovieScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MovieScoreServiceImpl extends ServiceImpl<MovieScoreMapper, MovieScore> implements MovieScoreService {

    @Autowired
    MovieScoreMapper movieScoreMapper;

    @Override
    public boolean score(UserToken userToken, ScoreDto scoreDto) {
        MovieScore movieScore = new MovieScore();
        movieScore.setMovieId(scoreDto.getId());
        movieScore.setScore(scoreDto.getScore());
        movieScore.setUserId(userToken.getUserId());
        LambdaQueryChainWrapper<MovieScore> qw = this.lambdaQuery().eq(MovieScore::getMovieId, movieScore.getMovieId()).eq(MovieScore::getUserId, movieScore.getUserId());
        MovieScore current = qw.one();
        if(current!=null){
            movieScore.setId(current.getId());
           return this.updateById(movieScore);
        }
        return this.save(movieScore);
    }
}
